package cn.dnui_xqh414.dnuifood_16110100414.Fragment;


import android.content.SharedPreferences;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;
import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.dnui_xqh414.dnuifood_16110100414.R;
import cn.dnui_xqh414.dnuifood_16110100414.bean.GetAllCollectBean;
import cn.dnui_xqh414.dnuifood_16110100414.fragment_adapter_and_vhoder.rv_Adapter;
import cn.dnui_xqh414.dnuifood_16110100414.fragment_adapter_and_vhoder.rv_Adapter_shopCollect;
import cn.dnui_xqh414.dnuifood_16110100414.listener.OnRetrofitListener;
import cn.dnui_xqh414.dnuifood_16110100414.model.ShopModel;
import cn.dnui_xqh414.dnuifood_16110100414.model.UserModel;

import static android.content.Context.MODE_PRIVATE;

/**
 * A simple {@link androidx.fragment.app.Fragment} subclass.
 */
public class Collect_shopFragment
        extends androidx.fragment.app.Fragment
        implements OnRetrofitListener<List<GetAllCollectBean>> {

    private rv_Adapter_shopCollect adapter;
    private List<GetAllCollectBean> list;

    public Collect_shopFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_collect_shop, container, false);
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        adapter.notifyDataSetChanged();
        adapter.setList(list);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        SharedPreferences sharedPreferences = getActivity()
                .getSharedPreferences("userinfo",MODE_PRIVATE);
        int userid = sharedPreferences.getInt("userid",-1);

        Log.e("*********",userid+"");
        ShopModel shopModel = new ShopModel();
        shopModel.getAllCollect(userid, 0, this);

        RecyclerView recyclerView = view.findViewById(R.id.rv_collect_shop);
        RecyclerView.LayoutManager layoutManager =
                new LinearLayoutManager(getContext());

        recyclerView.setLayoutManager(layoutManager);

        recyclerView.setHasFixedSize(true);

        adapter = new rv_Adapter_shopCollect(getContext());
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onSuccess(List<GetAllCollectBean> object, int flag) {
        adapter.setList(object);
        this.list = object;
    }

    @Override
    public void onFailure(String msg) {

    }
}
